package com.billy.ohos.loadingstatusview.wrapcomponent;

import com.billy.ohos.loading.Gloading;
import com.billy.ohos.loadingstatusview.ResourceTable;
import com.billy.ohos.loadingstatusview.util.Util;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.element.Element;

import java.util.ArrayList;

/**
 * demo:
 * bind status view to all item views in ListContainer (All AdapterView are the same)
 *
 * @author billy.qi
 * @since 19/3/19 23:20
 */
public class WrapListContainerAbility extends Ability {
    private ListHolder listHolder;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        listHolder = new ListHolder(this, initData());
        setUIContent(listHolder.createComponent());
    }

    private ArrayList<String> initData() {
        ArrayList<String> data = new ArrayList<>();
        data.add(Util.getErrorImage());
        data.add(Util.getRandomImage1());
        data.add(Util.getRandomImage2());
        data.add(Util.getErrorImage());
        data.add(Util.getRandomImage1());
        data.add(Util.getRandomImage2());
        data.add(Util.getRandomImage1());
        data.add(Util.getRandomImage2());
        data.add(Util.getRandomImage1());
        data.add(Util.getRandomImage2());
        return data;
    }

    class ListHolder {
        private Ability ability;
        private ComponentContainer mRootLayout;
        private ListContainer listContainer;
        private ListItemProvider listItemProvider;

        ListHolder(Ability ability, ArrayList<String> data) {
            this.ability = ability;
            listItemProvider = new ListItemProvider(ability, data);
        }

        public ComponentContainer createComponent() {
            Component mainComponent = LayoutScatter.getInstance(ability).parse(ResourceTable.Layout_ability_wrap_list, null, false);
            if (!(mainComponent instanceof ComponentContainer)) {
                return null;
            }
            mRootLayout = (ComponentContainer) mainComponent;
            listContainer = (ListContainer) mRootLayout.findComponentById(ResourceTable.Id_list);
            listContainer.setItemProvider(listItemProvider);
            return mRootLayout;
        }
    }

    class ListItemProvider extends RecycleItemProvider {
        private ArrayList<String> list;
        private int size;

        ListItemProvider(Ability ability, ArrayList<String> list) {
            this.list = list;
            size = ability.getResourceManager().getDeviceCapability().screenDensity / 2;
            size = 540;
            initData();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public String getItem(int position) {
            return list.get(position);
        }

        @Override
        public Component getComponent(int position, Component convertView, ComponentContainer parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                Image imageView = new Image(parent.getContext());
                imageView.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, size));
                Gloading.Holder holder = Gloading.getDefault().wrap(imageView);
                viewHolder = new ViewHolder(holder, imageView);
                // use wrapper view instead of origin image
                convertView = holder.getWrapper();
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.showImage(list.get(position));
            return convertView;
        }

        class ViewHolder implements Runnable {
            private final Gloading.Holder holder;
            Image image;
            private String curUrl;

            ViewHolder(Gloading.Holder holder, Image image) {
                this.image = image;
                this.holder = holder;
                this.holder.withRetry(this);
            }

            void showImage(String url) {
                curUrl = url;
                holder.showLoading();
                getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(WrapListContainerAbility.this)
                                .load(url)
                                .listener(new RequestListener<Element>() {
                                    @Override
                                    public boolean onLoadFailed(GlideException exception, Object obj, Target<Element> target, boolean boo) {
                                        holder.showLoadFailed();
                                        return false;
                                    }

                                    @Override
                                    public boolean onResourceReady(Element element, Object obj, Target<Element> target, DataSource dataSource, boolean boo) {
                                        holder.showLoadSuccess();
                                        return false;
                                    }
                                })
                                .into(image);
                    }
                }, 50);
            }

            @Override
            public void run() {
                showImage(curUrl);
            }

        }
    }

}
